# from django.contrib import admin
# from django import forms
# from django.contrib.auth.admin import UserAdmin
# from django.contrib.auth.forms import ReadOnlyPasswordHashField
# from django.contrib.auth.models import Group
# from easy_select2 import Select2Multiple
# from records.models import Record
# from users.models import UserProfile
#
#
# # 创建用户页
# class UserCreationForm(forms.ModelForm):
#     """A form for creating new users. Includes all the required
#     fields, plus a repeated password."""
#     password1 = forms.CharField(label='密码', widget=forms.PasswordInput)
#     password2 = forms.CharField(label='确认密码', widget=forms.PasswordInput)
#     records = forms.ModelMultipleChoiceField(label='病史', widget=Select2Multiple(
#         select2attrs={'width': '250px'}
#     ), queryset=Record.objects.all(), required=False)
#
#     class Meta:
#         model = UserProfile
#         fields = ('username', 'phoneNumber', 'name', 'email', 'sex', 'dateOfBirth', 'occupation', 'address')
#
#     def clean_password2(self):
#         # Check that the two password entries match
#         password1 = self.cleaned_data.get("password1")
#         password2 = self.cleaned_data.get("password2")
#         if password1 and password2 and password1 != password2:
#             raise forms.ValidationError("Passwords don't match")
#         return password2
#
#     def save(self, commit=True):
#         # Save the provided password in hashed format
#         print("asd123")
#         user = super(UserCreationForm, self).save(commit=False)
#         user.set_password(self.cleaned_data["password1"])
#         if commit:
#             user.save()
#         return user
#
#
# # 修改用户页
# class UserChangeForm(forms.ModelForm):
#     """A form for updating users. Includes all the fields on
#     the user, but replaces the password field with admin's
#     password hash display field.
#     """
#     password = ReadOnlyPasswordHashField()
#     records = forms.ModelMultipleChoiceField(label='病史', widget=Select2Multiple(
#         select2attrs={'width': '250px'}
#     ), queryset=Record.objects.all(), required=False)
#
#     class Meta:
#         model = UserProfile
#
#         fields = ('username', 'phoneNumber', 'name', 'email', 'password', 'sex', 'dateOfBirth',
#                   'occupation', 'address', 'is_admin', 'records')
#
#     def clean_password(self):
#         # Regardless of what the user provides, return the initial value.
#         # This is done here, rather than on the field, because the
#         # field does not have access to the initial value
#         print("asd")
#         return self.initial["password"]
#
#
# # 用户模型的管理器
# @admin.register(UserProfile)
# class UserProfileAdmin(UserAdmin):
#     # # list_display设置要显示在列表中的字段（id字段是Django模型的默认主键）
#     # list_display = ('name', 'sex', 'age', 'occupation', 'phoneNumber', 'address', 'preDeposit', 'totalConsumption', 'discount')
#     #
#     # # readonly_fields = ('age',)
#     #
#     # # list_per_page设置每页显示多少条记录，默认是100条
#     # list_per_page = 100
#     #
#     # # ordering设置默认排序字段，负号表示降序排序
#     # ordering = ('-createdAt',)
#     #
#     # # list_editable 设置默认可编辑字段
#     # # list_editable = ['name']
#     #
#     # # fk_fields 设置显示外键字段
#     # # fk_fields = ('machine_room_id',)
#     #
#     # # search_fields 添加搜索
#     # search_fields =['name', 'phoneNumber']
#     #
#     # # list_filter 添加左边栏过滤器
#     # # list_filter = ['pub_date']
#     # The forms to add and change user instances
#     form = UserChangeForm
#     add_form = UserCreationForm
#
#
#
#     # The fields to be used in displaying the User model.
#     # These override the definitions on the base UserAdmin
#     # that reference specific fields on auth.User.
#     list_display = ('name', 'username', 'email', 'phoneNumber', 'age', 'is_superuser')
#     list_filter = ('is_admin', 'groups',)
#     fieldsets = (
#         (None, {'fields': ('username', 'phoneNumber', 'name', 'email', 'password')}),
#         ('基本信息', {'fields': ('sex', 'dateOfBirth', 'occupation', 'address', 'records')}),
#         ('权限', {'fields': ('is_admin', 'groups', 'user_permissions',)}),
#     )
#     # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
#     # overrides get_fieldsets to use this attribute when creating a user.
#     add_fieldsets = (
#         (None, {
#             'classes': ('wide',),
#             'fields': ('username', 'phoneNumber', 'name', 'email', 'password1', 'password2', 'sex', 'dateOfBirth',
#                   'occupation', 'address', 'records', 'is_admin')}
#          ),
#     )
#     search_fields = ('email', 'phoneNumber', 'username', 'name')
#     ordering = ('-id',)
#     filter_horizontal = ('groups', 'user_permissions',)
#
